package com.yuqiaotech.zcj.auth.action;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport;
import com.yuqiaotech.zcj.Constants;
import com.yuqiaotech.zcj.auth.model.User;
import com.yuqiaotech.zcj.dao.BaseDao;
import com.yuqiaotech.zcj.forum.model.Category;
import com.yuqiaotech.zcj.forum.model.Forum;
import com.yuqiaotech.zcj.service.GenericManager;
import com.yuqiaotech.zcj.service.UserManager;

@ParentPackage("normal")
@Results( {
		@Result(name = "login", value = "/login.jsp"),
		@Result(name = "categoryList", value = "/forum/categoryList.jsp"),
		@Result(name = "categoryEditForm", value = "/forum/categoryEditForm.jsp"),
		@Result(name = "listTopics", value = "/forum/listTopics.jsp")

})
public class IndexAction extends ActionSupport {
	private User user;
	private UserManager userManager;
	private List<Category> categories;
	private Category category;
	private BaseDao baseDao;
	private GenericManager<Category, Long> categoryManager;
	private GenericManager<Forum, Long> forumManager;
	private List<Forum> forums;
	private Forum forum;
	private long forumId;

     public void category(){
    	 DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
    	 categories = baseDao.findByCriteria(dc);
    	// categories = categoryManager.findByHql("from Category");
     }
	public String execute() {
		category();
		return "login";
	}

	public String logout() {
		HttpSession session = ServletActionContext.getRequest().getSession(
				false);
		if (session != null) {
			session.invalidate();
		}
		ServletActionContext.getRequest().getSession(true);
		return execute();
	}

	public String login() {

		User u = userManager.findUniqueBy(User.class, "username", user
				.getUsername());
		if (u != null && u.getPassword().equals(user.getPassword())) {
			ServletActionContext.getRequest().getSession().setAttribute(
					Constants.USER_KEY, u);
			return execute();
		} else {
			return execute();
		}
	}

	public String listTopics() {
		System.out.println(forumId);
		forum = forumManager.get(forumId);
		return "listTopics";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public GenericManager<Category, Long> getCategoryManager() {
		return categoryManager;
	}

	public void setCategoryManager(
			GenericManager<Category, Long> categoryManager) {
		this.categoryManager = categoryManager;
	}

	public GenericManager<Forum, Long> getForumManager() {
		return forumManager;
	}

	public void setForumManager(GenericManager<Forum, Long> forumManager) {
		this.forumManager = forumManager;
	}

	public List<Forum> getForums() {
		return forums;
	}

	public void setForums(List<Forum> forums) {
		this.forums = forums;
	}

	public Forum getForum() {
		return forum;
	}

	public void setForum(Forum forum) {
		this.forum = forum;
	}
	public long getForumId() {
		return forumId;
	}

	public void setForumId(long forumId) {
		this.forumId = forumId;
	}

}
